/**
 * @filename index.js
 * @directory /WebRoot/jsp/system/Hospital
 * @description 医院管理
 * @author 
 * @date 2013-02-19 10:00:59
 * @version v0.1
*/

var view_win;

$(function() {
	$(".context").height($(window).height() - 70);
	$("#center-grid-data").simplegrid();
});


/**
 * 添加
 */
function btn_add() {
	$("#hospital-button").unbind();
	$("#hospital-button").click(exec_add);
	$("#hospital-form")[0].reset();
	$("#hospital-div").window({title: '添加', iconCls:'icon-add', closed: false});
}

function exec_add() {
	var data = $("#hospital-form").serializeArray();
	$.ajax({
		url: 'system/Hospital/add',
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(data) {
			if(data.result > 0) {
				alert("添加成功");
				$("#center-grid-data").simplegrid("refresh");
			} else {
				this.error();
			}
		},
		error: function() {
			alert("添加失败");
		}
	});
}

/** 编辑 */
function btn_edit() {
	var hospital = $("#center-grid-data").simplegrid("getSelected");
	if (hospital.length == 0) {
		alert("请选择一条记录.");
		return;
	}
	hospital = hospital[0];
	$("#hospital-button").unbind();
	$("#hospital-button").click(exec_edit);
	$("#hospital-form").form("load", {
		'hospital.bh': hospital.bh,
		'hospital.mingc': hospital.mingc,
		'hospital.jianc': hospital.jianc,
		'hospital.suosd': hospital.suosd,
		'hospital.dianh': hospital.dianh,
		'hospital.dizhi': hospital.dizhi
	});
	$("#hospital-div").window({title: '修改', iconCls:'icon-edit', closed: false});
}

function exec_edit() {
	var data = $("#hospital-form").serializeArray();
	$.ajax({
		url: 'system/Hospital/edit',
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(data) {
			if(data.result > 0) {
				alert("编辑成功");
				$("#center-grid-data").simplegrid("refresh");
			} else {
				this.error();
			}
		},
		error: function() {
			alert("编辑失败");
		}
	});
}

/** 删除 */
function btn_delete() {
	var hospital = $("#center-grid-data").simplegrid("getSelected");
	if (hospital.length == 0) {
		alert("没有选择记录.");
		return;
	}
	hospital = hospital[0];
	if (confirm("确认删除[" + hospital.mingc + "]吗?")) {
		$.ajax({
			url: 'system/Hospital/del',
			type: 'post',
			data: {bh: hospital.bh},
			success: function() {
				alert("删除成功");
				$("#center-grid-data").simplegrid("refresh");
			},
			error: function() {
				alert("删除失败");
			}
		});
	}
}